package com.budwk.app.miniapp.utils;

import cn.hutool.core.util.NumberUtil;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

public class DistanceUtils {

    //计算两个经纬度之间的距离
    public static double latitudeLongitudeDistance(double lat1, double lon1, double lat2, double lon2) {
        final int R = 6371; // 地球平均半径，单位为公里
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
            Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double distance = R * c * 1000; // 将单位从公里转换为米
        return distance;
    }




    public static void main(String[] args) {



//        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(
//                Ellipsoid.Sphere,
//                new GlobalCoordinates(26.595694, 106.581733),
//                new GlobalCoordinates(26.597846, 106.602968)
//        );
//
//        System.out.println(geoCurve.getEllipsoidalDistance());
      double d= latitudeLongitudeDistance(26.595694, 106.581733, 26.597846, 106.602968);
        System.out.println(NumberUtil.roundStr(d/1000,1));
    }
}
